﻿using BrnMall.Manager.Domain.WeChat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.WeChat
{
    public class PermanentCodeManager
    {
        public static void Add(PermanentCodeInfo model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
        }

        //public static PermanentCodeInfo GetByStoreId(int storeId)
        //{
        //    using (var db = new QDMallDatabase())
        //    {
        //        return db.SingleOrDefault<PermanentCodeInfo>($"select top 1 * from wechat_PermanentCode where storeid={storeId} order by id desc");
        //    }
        //}

        /// <summary>
        /// 获取企业微信授权记录
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="appName">应用名称</param>
        /// <returns></returns>
        public static PermanentCodeInfo GetByStoreId(int storeId, string appName)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<PermanentCodeInfo>($"select top 1 * from wechat_PermanentCode where storeid={storeId} and app_name='{appName}' order by id desc");
            }
        }

        public static PermanentCodeInfo GetByCorpid(string corpid)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<PermanentCodeInfo>($"select top 1 * from wechat_PermanentCode where corpid='{corpid}' order by id desc");
            }
        }
        public static bool UpdateAccessToken(int id,string access_token,DateTime last_time)
        {
            int count = 0;
            using (var db = new QDMallDatabase())
            {
                count= db.Execute("update wechat_PermanentCode set access_token=@0,last_expires=@1 where id=@2",access_token,last_time,id);
            }
            return count > 0;
        }
    }
}